#ifndef _CCSV_WRITER_H_
#define _CCSV_WRITER_H_
#include "toolLib.h"
#include <stdio.h>
#include <stdlib.h>
#include "csvRecord.h"
#include <ctype.h>
#include <string.h>
typedef struct csvWriter *CsvWriter;
typedef enum
{
    CCSVWriteErrorWriterNoError,
    CCSVWriteErrorWriterAttrError,
    CCSVWriteErrorArgErr,
    CCSVWriteErrorWriteEOF,
    CCSVWriteErrorWriteError,
    CCSVWriteErrorFieldNumError,
    CCSVWriteErrorFieldFormatError,
    CCSVWriteErrorRawSizeOutOfLimit,
} CCSVWriteError;
//a csvWriter writes records using CSV encoding.
//
//As returned by NewWriter, a Writer writes records terminated by a newline and use ',' as the field delimiter.
//The exported fields can be changed to customize the details before the first call to Write or WriteAll.
//
//comma is the field delimiter
//
//If useCRLF is true, the Writer ends each output line with \r\n instead of \n.
//
//The writes of individual records are buffered.
//After all data has been written, the client should call the Flush method to guarantee all data has been forwarded to
//the underlying fp. Any errors that occurred should be write to csvWriter.error
struct csvWriter
{
    char comma;
    BOOL useCRLF;
    FILE *fp;
    //we initialize the buffer when we call NewCCSVReader
    char *rawBuffer;
    //rawBuffer is a line buffer only used by the readLine method
    //assign the sizeof rawBuffer
    char *nextFieldIndex;
    int rawBufferSize;
    int rawLen;
};
void ReleaseCCSVWriter(CsvWriter writer);
CsvWriter NewCCSVWriter(FILE *fp);
CCSVWriteError CCSVWrite(CsvWriter w, CsvRecord r);
void UseCRLF(CsvWriter w);
#endif